package com.swf.seed.os.command.impl;

import java.io.File;
import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FilenameUtils;

import com.swf.seed.base.SdLetter;
import com.swf.seed.base.SystemContants;
import com.swf.seed.os.annotation.Command;
import com.swf.seed.os.util.FTPUtil;
import com.swf.seed.os.util.SFTPUtil;

/**
 * <p>
 * ftp -u username -p password -host 127.0.0.1 -port 21 -get D:\linux\test\a.zip /home/sftpuser/download/a.zip
 * </p>
 *
 * @author Weifeng Shen
 * @since 2016年7月21日
 * @see
 */
@Command("ftp")
public class FTPCommand extends CommandAdaptor {
	
	

	@Override
	public void option() {
		StringBuilder builder = new StringBuilder();
		builder.append("Description:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  supported ftp").append(SystemContants.LINE_SEPARATOR);
		builder.append("Example:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  get:ftp -u username -p password -host 127.0.0.1 -port 22 -get D:/a.zip /download/a.zip").append(SystemContants.LINE_SEPARATOR);
		builder.append("  put:ftp -u username -p password -host 127.0.0.1 -port 22 -put D:/a.zip /download/a.zip").append(SystemContants.LINE_SEPARATOR);
		footer = builder.append(super.footer);
		options.addOption(SdLetter.h, "help", false, "show sftp's usage");
		options.addOption(Option.builder(SdLetter.u).longOpt("username").argName("username").desc("ftp username").hasArg().build());
		options.addOption(Option.builder(SdLetter.p).longOpt("password").argName("password").desc("ftp password").hasArg().build());
		options.addOption(Option.builder(SdLetter.f).longOpt("cf").argName("configFile").desc("file config ftp info").hasArg().build());
		options.addOption(Option.builder("port").longOpt("port").argName("port").desc("ftp port like 22").hasArg().build());
		options.addOption(Option.builder("host").longOpt("host").argName("host").desc("ftp host like 127.0.0.1").hasArg().build());
		options.addOption(Option.builder("get").longOpt("get").numberOfArgs(2).argName("localFile,remoteFile").desc("two args:localFile,remoteFile").build());
		options.addOption(Option.builder("put").longOpt("put").numberOfArgs(2).argName("localFile,remoteFile").desc("two args:localFile,remoteFile").build());
	}

	@Override
	void customizedExecute(CommandLine commandLine) throws Exception {
		String userName = null;
		String password = null;
		int port = 21;
		String host = "127.0.0.1";
		if (commandLine.hasOption(SdLetter.f)){
			String configFile = commandLine.getOptionValue(SdLetter.f);
			
		}
		if (commandLine.hasOption(SdLetter.u)){
			userName = commandLine.getOptionValue(SdLetter.u);
		}
		if (commandLine.hasOption(SdLetter.p)){
			password = commandLine.getOptionValue(SdLetter.p);
		}
		if (commandLine.hasOption("port")){
			port = Integer.parseInt(commandLine.getOptionValue("port"));
		}
		if (commandLine.hasOption("host")){
			host = commandLine.getOptionValue("host");
		}
		
		System.out.println("sftp ["+userName+"@"+password+"]@"+host+":"+port);
		FTPUtil ftpUtil = new FTPUtil();
		ftpUtil.setUsername(userName);
		ftpUtil.setPassword(password);
		ftpUtil.setPort(port);
		ftpUtil.setHost(host);
		ftpUtil.connect(host, port, userName, password);
		if (commandLine.hasOption("get")){
			String[] files = commandLine.getOptionValues("get");
			if (files.length<2){
				System.err.println("get need two args!");
				return;
			}
			String localFile = files[0];
			String remoteFile = files[1];
			try {
			if (localFile.matches(".+?\\.[A-Za-z]+")){
				if (!new File(localFile).getParentFile().exists()){
					new File(localFile).getParentFile().mkdirs();
				}
			}else{
				if (!new File(localFile).exists()){
					new File(localFile).mkdirs();
				}
				localFile = new File(localFile,FilenameUtils.getName(remoteFile)).getAbsolutePath();
			}
			if (ftpUtil.get(remoteFile,localFile)){
				System.out.println(String.format("下载文件%s到%s成功",remoteFile,localFile));
			};
			} catch (IOException e) {
				throw new ParseException(e.getMessage());
			}
		}
		if (commandLine.hasOption("put")){
			String[] files = commandLine.getOptionValues("put");
			if (files.length<2){
				System.err.println("get need two args!");
				return;
			}
			String localFile = files[0];
			String remoteFile = files[1];
			try {
				if (!remoteFile.matches(".+?\\.[A-Za-z]+")){
					remoteFile = FilenameUtils.separatorsToUnix(FilenameUtils.concat(remoteFile,FilenameUtils.getName(localFile)));
				}
				if (ftpUtil.put(localFile,remoteFile)){
					System.out.println(String.format("上传文件%s到%s成功", localFile,remoteFile));
				};
			} catch (Exception e) {
				throw new ParseException(e.getMessage());
			}finally{
				ftpUtil.disconnect();
			}
		}
	}

}
